package face.pay.wx.controller;

import com.alibaba.fastjson.JSON;
import face.pay.constant.CacheKey;
import face.pay.entity.CpnWxAppInfo;
import face.pay.redis.RedisUtils;
import face.pay.util.JSSDK_Config;
import face.pay.util.ResultMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/wx_jsSdk")
@Slf4j
public class JsApiConfigController {
    @Autowired
    RedisUtils redisUtils;


    @RequestMapping("/config")
    public ResultMessage JSSDK_config(@RequestParam(value = "url", required = true) String url) {
        try {
            log.info("获取微信配置");
            log.info("url:"+url);
            String json  =  (String) redisUtils.get(CacheKey.WX_APP_INFO);
            CpnWxAppInfo cpnWxAppInfo = JSON.parseObject(json,CpnWxAppInfo.class);
            Map<String, String> configMap = JSSDK_Config.jsSDK_Sign(url,cpnWxAppInfo);
            return ResultMessage.createSuccess(configMap);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMessage.createFail();
        }
    }
}
